package kr.hkit.powerjava.chapter18;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel_JComboBox extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private JTextField tfAdd;
	private JTextField tfSelected;
	private JButton btnAdd;
	private JComboBox<String> comboBox;

	/**
	 * Create the panel.
	 */
	public Panel_JComboBox() {

		initialize();
	}
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Top = new JPanel();
		add(panel_Top, BorderLayout.NORTH);
		
		tfAdd = new JTextField();
		panel_Top.add(tfAdd);
		tfAdd.setColumns(10);
		
		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		panel_Top.add(btnAdd);
		
		JPanel panel_Center = new JPanel();
		add(panel_Center, BorderLayout.CENTER);
		
		comboBox = new JComboBox<String>();
		comboBox.addActionListener(this);
		panel_Center.add(comboBox);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		JLabel lblNewLabel = new JLabel("선택된 항목");
		panel_Bottom.add(lblNewLabel);
		
		tfSelected = new JTextField();
		tfSelected.setEditable(false);
		panel_Bottom.add(tfSelected);
		tfSelected.setColumns(10);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == comboBox) {
			actionPerformedComboBox(e);
		}
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	protected void actionPerformedBtnAdd(ActionEvent e) {
		comboBox.addItem(tfAdd.getText().trim());
		tfAdd.setText("");
		tfAdd.requestFocus();
	}
	protected void actionPerformedComboBox(ActionEvent e) {
		tfSelected.setText((String)comboBox.getSelectedItem());
	}
}
